<?php
/**
 * Die Captcha-Klasse von dem LD-CMS2
 * -Erstellen eines Captcha-Bildes
 * 
 * @param none
 * @author Dennis Franke <dennis@franke-s.de>
 * @package LD-CMS2
 */
class Captcha {

	/**
	 * Create Randomcode
	 *
	 * @param int $anz Length of the Code
	 * @return string $out Randomcode
	 */
	public function genCode($anz = 5) {
		$st1 = range('2','9');
		$st2 = range('A','Z');
		// Delete unknown signs
		unset($st2[array_search("I",$st2)]);
		unset($st2[array_search("O",$st2)]);
		// Merge array and shuffle
		$temp = array_merge( $st1, $st2 );
        srand ( (float)microtime() * 1000000);
        shuffle ( $temp );
        $out = '';
		$cc = 0;
        foreach ( $temp as $v ) {
        	if ($cc++ >= $anz) break;
        	$out .= $v;
    	}
		return $out;
	}
	
	/**
	 * Get randomcolor
	 *
	 * @param string $image
	 * @return string $color Randomcolor
	 */
	public function genColor($image) {
		$col_r = range ( 10, 200 );
        $col_g = range ( 10, 200 );
        $col_b = range ( 10, 200 );
        srand ( (float)microtime() * 1000000);
        shuffle ( $col_r );
        shuffle ( $col_g );
        shuffle ( $col_b );
        $color = ImageColorAllocate($image, $col_r[0], $col_g[0], $col_b[0]);
        return $color;
	}
	
	/**
	 * Get Random-Angle
	 *
	 * @return unknown
	 */
	function genDegree() {
        $degree = range ( -40, 40);
        srand ( (float)microtime() * 1000000);
        shuffle ( $degree );
        return $degree[1];
    }
	
	/**
	 * Create Image
	 *
	 * @param string $width Width of the picture
	 * @param string $height Height of the picture
	 * @param string $code Generated Code
	 * @param string $pfadZuFont Path to the font
	 * @return bool
	 */
	function createImg($width,$height,$code,$pathToFont) {
		// neue Grafik estellen
    	$im = ImageCreateTrueColor( $width, $height);

   		// Hintergrundfarbe einstellen
    	$WEISS = ImageColorAllocate($im, 255, 255, 255); 
    	ImageFill($im, 0, 0, $WEISS );
		
		// Linien zeichnen 
		
		$black = ImageColorAllocate($im, 158, 158, 158); 
		$number = round($width/20);
		for ($i=0;$i <= $number;$i++) {
			$range=range((($i*20) - 5),(($i*20) + 5));
			srand ( (float)microtime() * 1000000);
        	shuffle ( $range );
			imageline($im, $range[0], 0,$range[1],$height, $black );
		}
		
		$black  = ImageColorAllocate($im, 158, 158, 158); 
		$number = round($height/20);
		for( $i=0;$i <= $number;$i++) {
			$range = range((($i*20) - 5),(($i*20) + 5));
			srand ( (float)microtime() * 1000000);
        	shuffle ( $range );
			imageline($im, 0, $range[0],$width,$range[1], $black );
		}

   		// Insert Code
    	$text = $code;
    	$text_count = strlen($text);
    	$step = 15;
    	for ( $i = 0; $i < $text_count; $i++) {
    	    
			// Select Font
			$TTF = "captchaFont.tff";
			$FONT = $pathToFont.$TTF;
			
			// Get Font-width
			$TS = imagettfbbox( 26, 0, $FONT, $text{$i} );
			$x = abs($TS[2]-$TS[0]);
	
			// Letter too small?
			if ($x < 13) $x = 15;
	
			// Output text
			ImageTTFText($im, 26, $this->genDegree(), $step, 30, $this->genColor($im), $FONT, $text{$i} );
			// Schrittweite einstellen
			$step += ($x+10);
	
		}

		// Save Picture
		ImageJPEG ($im, $pathToFont.'/$$$.jpg', 20);
		ImageDestroy($im);
		return true;
	}
	
}
/*
$captcha = new Captcha;
$code = $captcha->genCode();
if($captcha->createImg('160','40',$code,"./")) {
	echo '<img src="captcha/$$$.jpg" border="1">'.$code;
}
*/
?>